/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaclientcp3110.UCM;
import common.UCM.UCController;
import javaclientcp3110.Model.Component.CompDeleteModel;
import javaclientcp3110.Model.MainModel;
import javaclientcp3110.View.Component.CompDeleteView;
/**
 *UC Delete Component
 *
 * Pre-condition: A valid user is logged into the system.
 *
 *  1. User selects to delete a component
 *  2. System displays a list of component types
 *  3. User selects component type
 *  4. System displays a list of components of that type
 *  5. User select component and clicks delete
 *  6. System deletes component from the storage medium
 *
 * Post-condition: The component is deleted from the system
 *
 * Unsuccessful Outcomes:
 *
 *   * 6a) System is unable to update the storage medium
 *        1. System displays an error message
 */
public class UCDeleteComponent implements UCController{
    private CompDeleteView view;
    private CompDeleteModel model;
    /**
     * Defualt constructor, creates new model and view
     */
    public UCDeleteComponent() {
        this (new CompDeleteView(), new CompDeleteModel());
    }
    /**
     * Constructor that sets the model and view to the inputs
     * @param view - view to set to
     * @param model - model to set to
     */
    public UCDeleteComponent(CompDeleteView view, CompDeleteModel model) {
        this.view = view;
        this.model = model;
    }
    /**
     * Implement uccontrollers run function. This is the body of the use case, it
     * is where the use case is run.
     */
    public void run() {
    //*  1. User selects to delete a component
    //*  2. System displays a list of component types
    //*  3. User selects component type
    //*  4. System displays a list of components of that type
    //*  5. User select component and clicks delete
    //*  6. System deletes component from the storage medium
        model.setDeleteComponent(view.getComponent());
        boolean itemDeleted = model.deleteComponent();
        if ( itemDeleted != true ) {
    //* 6a) System is unable to update the storage medium
    //*        1. System displays an error message
            view.errorMessage("The component could not be deleted");
        }
        else {
            view.successMessage("Component successfully deleted!");
            MainModel.getInstance().updateData();
            view.updateComponentList();
        }
    }
}
